package com.chinanet.service.faultanticipate.flowmodel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
 * Created by moon on 16/9/27.
 */
public class FlowModel {
    public Set<Map.Entry<String, Object>> nodes;
    public Set<Map.Entry<String, Object>> lines;

    public FlowModel(String json){
        JSONObject out = JSON.parseObject(json);
        nodes = ((JSONObject)out.get("nodes")).entrySet();
        lines = ((JSONObject)out.get("lines")).entrySet();
    }

    //通过出发节点找抵达节点
    public ArrayList<Line> findTobyFrom(String name){
        ArrayList<Line> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : lines) {
            JSONObject tp = (JSONObject) entry.getValue();
            if(tp.getString("from").equals(name)){
                list.add(new Line(entry.getKey(),tp.getString("to")));
            }
        }
        return list;
    }

    //通过ID找到节点
    public JSONObject findNode(String id){
        for (Map.Entry<String, Object> entry : nodes) {
            if(entry.getKey().equals(id)){
                return (JSONObject)entry.getValue();
            }
        }
        return null;
    }
}
